import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:gif/gif.dart';
import 'package:lottie/lottie.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';

import 'base/wqs_text.dart';

const _kDefaultSquatsTriggerOffset = 190.0;

class BallHeader extends Header {
  final Key? key;

  /// Background color.
  final Color? backgroundColor;

  const BallHeader({
    this.key,
    bool clamping = false,
    double triggerOffset = _kDefaultSquatsTriggerOffset,
    IndicatorPosition position = IndicatorPosition.above,
    Duration processedDuration = Duration.zero,
    SpringDescription? spring,
    SpringBuilder? readySpringBuilder,
    bool springRebound = false,
    FrictionFactor? frictionFactor,
    double? infiniteOffset,
    bool? hitOver,
    bool? infiniteHitOver,
    bool hapticFeedback = false,
    this.backgroundColor,
  }) : super(
          triggerOffset: triggerOffset,
          clamping: clamping,
          processedDuration: processedDuration,
          spring: spring,
          readySpringBuilder: readySpringBuilder,
          springRebound: springRebound,
          frictionFactor: frictionFactor,
          safeArea: false,
          infiniteOffset: infiniteOffset,
          hitOver: hitOver,
          infiniteHitOver: infiniteHitOver,
          position: position,
          hapticFeedback: hapticFeedback,
        );

  @override
  Widget build(BuildContext context, IndicatorState state) {
    assert(state.axis == Axis.vertical,
        'SquatsHeader does not support horizontal scrolling.');
    return RefreshIndicator(
      key: key,
      state: state,
      reverse: state.reverse,
      backgroundColor: backgroundColor,
    );
  }
}

class RefreshIndicator extends StatefulWidget {
  final IndicatorState state;
  final bool reverse;
  final Color? backgroundColor;

  const RefreshIndicator({
    super.key,
    required this.state,
    required this.reverse,
    this.backgroundColor,
  });

  @override
  State<RefreshIndicator> createState() => _RefreshIndicatorState();
}

class _RefreshIndicatorState extends State<RefreshIndicator>
    with TickerProviderStateMixin {
  late final AnimationController controller;

  double get _offset => widget.state.offset;
  String text = "下拉刷新";

  @override
  void initState() {
    super.initState();
    controller = AnimationController(vsync: this);
    widget.state.notifier.addModeChangeListener(_onModeChange);
  }

  @override
  void dispose() {
    widget.state.notifier.removeModeChangeListener(_onModeChange);
    controller.dispose();
    super.dispose();
  }

  void _onModeChange(IndicatorMode mode, double offset) {
    if (mode == IndicatorMode.processing) {
      setState(() {
        text = "正在刷新";
      });
    } else if (mode == IndicatorMode.drag) {
      setState(() {
        text = "下拉刷新";
      });
    } else if (mode == IndicatorMode.armed) {
      controller.repeat(period: const Duration(seconds: 1));
      setState(() {
        text = "松开刷新";
      });
    } else if (mode == IndicatorMode.processed || mode == IndicatorMode.done) {
      controller.stop();
      setState(() {
        text = "刷新完成";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      width: double.infinity,
      height: _offset,
      color: widget.backgroundColor ?? Colours.transparent,
      child: Stack(
        children: [
          Positioned(
            top: _offset - 84,
            left: 0,
            right: 0,
            child: Gif(
                image: AssetImage(Utils.getFilePath('refresh_loading.gif')),
                width: 40,
                height: 40,
                autostart: Autostart.loop),
          ),
          Positioned(
              top: _offset - 30,
              left: 0,
              right: 0,
              child: QsText(text,
                  textAlign: TextAlign.center,
                  fontSize: 12,
                  color: Colours.grey99))
        ],
      ),
    );
  }
}
